# MAKEFLAGS += -s
CROSS_COMPILE:=/home/duan/00_works/3_document/Ingenic/mips-gcc540-glibc222-r3.3.7.mxu2.mem03/bin/mips-linux-gnu-
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)gcc-ar
STRIP = $(CROSS_COMPILE)strip

# muclibc, mglibc
LIBTYPE ?= muclibc
# nv12, rgba/bgra
INTYPE ?= rgba
# static, dynamic
LIBSTATUS ?= dynamic

# 获取 Makefile 所在目录
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
CURRENT_DIR := $(dir $(mkfile_path))
OBJS_PATH = $(CURRENT_DIR)/objs
TNNA_LIB_PATH = $(CURRENT_DIR)/InferenceKit/uranus/tnna/

XBLIB_INCLUDE_PATH = $(CURRENT_DIR)/include/
XBLIB_INTERFACE_INC = -I$(XBLIB_INCLUDE_PATH)

STB_INCLUDE_PATH = $(CURRENT_DIR)/stb/
STB_INTERFACE_INC = -I$(STB_INCLUDE_PATH)

SRC_PATH = $(CURRENT_DIR)/src/
SRC = $(notdir $(wildcard $(SRC_PATH)/*.cpp))
OBJS_SRC = $(patsubst %.cpp,$(OBJS_PATH)/%.o,$(SRC))

Gen_LIB_PATH = $(CURRENT_DIR)/xb_libs

# 修改编译选项，确保-fPIC在编译和链接阶段都启用
DEF_OPTTIONS= -std=c++14 -mfp32 -Wall -EL -O3 -Os -march=mips32r2 -flax-vector-conversions -fPIC
LDFLAGS = -lpthread -lrt -ldl -lm -flto -Wl,-Map=object.map,--cref,--gc-section

CXXFLAGS := $(DEFEINE_OPT) -ffunction-sections -fdata-sections -fno-exceptions -fPIC

ifeq ($(INTYPE), nv12)
	CXXFLAGS += -DURANUS_NV12
endif

ifeq ($(LIBTYPE), muclibc)
    CXXFLAGS += -muclibc
	LIB_PATH = $(TNNA_LIB_PATH)/540/lib/uclibc
else
	LIB_PATH = $(TNNA_LIB_PATH)/540/lib/glibc
endif 

# 动态库名称
TARGET_LIB = lib_catface_recog.so

INCLUDES := -I$(TNNA_LIB_PATH) -I$(TNNA_LIB_PATH)/include $(XBLIB_INTERFACE_INC) $(STB_INTERFACE_INC)

$(OBJS_PATH)/%.o : $(SRC_PATH)/%.cpp
	@mkdir -p $(OBJS_PATH)
	$(CPP) $(DEF_OPTTIONS) $(CXXFLAGS) $(INCLUDES) -o $@ -c $<


# 生成动态库规则
$(TARGET_LIB): $(OBJS_SRC)
	@mkdir -p $(Gen_LIB_PATH)
	$(CPP) -shared $(CXXFLAGS) $^ -o $(Gen_LIB_PATH)/$@ $(LDFLAGS) $(INCLUDES) \
	-Wl,-soname,$(TARGET_LIB) \
	-L$(LIB_PATH) -luranus_tensor -luranus_core -luranus_extend -laie \
	-Wl,--no-whole-archive -Wl,--gc-sections -Wl,--export-dynamic

all: $(TARGET_LIB)
.PHONY: all clean
clean:
	rm -rf $(OBJS_PATH) $(UTILS_TARGET_LIB) $(YOLO_TARGET_LIB) ${Gen_LIB_PATH}